package controller;

import bean.dto.Dto;
import bean.pojo.GdComment;
import bean.pojo.GdUser;
import bean.vo.ArticleVo;
import bean.vo.CommentVo;
import bean.vo.DoctorVo;
import bean.vo.TokenVo;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.*;
import utils.DtoUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private GdUserService gdUserService;
    @Resource
    private TokenService tokenService;
    @Resource
    private GdUserDoctorlistService gdUserDocListServe;
    @Resource
    private GdDoctorService gdDoctorService;
    @Resource
    private GdUserArticlelistService gdUserArticlelistService;
    @Resource
    private GdCommentService gdCommentService;
    /**
     * 用户登录的方法
     * @param nickName
     * @param passWord
     * @param request
     * @return
     */
    @RequestMapping(value = "/userLogin",method = RequestMethod.GET)
    public @ResponseBody
    Dto userLogin(@RequestParam("nickName")String nickName,
                  @RequestParam("passWord")String passWord,
                  HttpServletRequest request){

        try {
            GdUser gdUser = gdUserService.selectByName(nickName);
            if (gdUser==null){

                return DtoUtil.returnFail("error","不存在此用户名");
            }
            if (!gdUser.getUserPassword().equals(passWord))
                return DtoUtil.returnFail("error","密码输入错误");

            String token = tokenService.generateToken(request.getHeader("user-agent"),gdUser);
            tokenService.saveToken(token,gdUser);
            TokenVo tokenVo = new TokenVo(token,Calendar.getInstance().getTimeInMillis()+TokenService.SESSION_TIMEOUT,
                    Calendar.getInstance().getTimeInMillis());
            return DtoUtil.returnSuccessWithData("登录成功",tokenVo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","登录失败");
        }
    }

    /**
     * 关注医生
     * @param doctorVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/followDoc",method =RequestMethod.POST)
    public @ResponseBody
    Dto followDoc(@RequestBody DoctorVo doctorVo, HttpServletRequest request){
        String token = request.getHeader("token");
        GdUser gdUser = null;
        try {
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                gdUser = tokenService.getUser(token);
                if (!gdUserDocListServe.isFollow(doctorVo.getDoctorId(),gdUser.getId())){//判断是否已关注
                    if (gdUserDocListServe.follow(gdUser.getId(),doctorVo.getDoctorId())){
                        gdDoctorService.updateFollower(doctorVo.getDoctorId(),doctorVo.getFollowers()+1);
                        return DtoUtil.returnSuccessWithMsg("关注成功");
                    }
                }
                return DtoUtil.returnSuccessWithMsg("已关注");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("error","关注失败");
    }

    /**
     * 取消对医生的关注
     * @param doctorVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/unFollowDoc",method = RequestMethod.POST)
    public @ResponseBody Dto unFollowDoc(@RequestBody DoctorVo doctorVo, HttpServletRequest request){
        String token = request.getHeader("token");
        GdUser gdUser = null;
        try {
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                gdUser = tokenService.getUser(token);
                if (gdUserDocListServe.unFollow(gdUser.getId(),doctorVo.getDoctorId())){
                    gdDoctorService.updateFollower(doctorVo.getDoctorId(),doctorVo.getFollowers()-1);
                    return DtoUtil.returnSuccessWithMsg("取消关注成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnFail("error","取消关注失败");
    }

    /**
     * 评价医生
     * @param request
     * @return
     */
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    public @ResponseBody Dto addComment(@RequestBody CommentVo commentVo,
                                        HttpServletRequest request){
        String token = request.getHeader("token");
        try {
            GdUser user = tokenService.getUser(token);
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                GdComment gdComment = new GdComment();
                gdComment.setDoctorId(commentVo.getDoctorId());
                gdComment.setUserId(user.getId());
                gdComment.setComment(commentVo.getComment());
                gdComment.setSatisfactionLevel(commentVo.getSatisfactionLevel());
                if (gdCommentService.insert(gdComment)){
                    return DtoUtil.returnSuccessWithMsg("评论成功");
                }
                return DtoUtil.returnFail("error","评论失败");
            }
            return DtoUtil.returnFail("error","token已失效");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","评论失败");
        }
    }

    /**
     * 注销的方法
     * @param request
     * @return
     */
    @RequestMapping(value = "/logOut",method = RequestMethod.GET)
    public @ResponseBody Dto logOut(HttpServletRequest request){
        String token =request.getHeader("token");
        try {
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                if (tokenService.delete(token)){
                    return DtoUtil.returnSuccessWithMsg("注销成功");
                }
                return DtoUtil.returnFail("error","注销失败");
            }
            return DtoUtil.returnFail("error","注销失败");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("error","注销失败");
        }
    }

}
